import { Effect, Reducer } from 'umi';
import { routerRedux } from 'dva/router';
import { login } from '@/services/login';
import { message } from 'antd';
import { history } from 'umi';
export interface UserModelState {
  username?: string;
  password?: string;
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    login: Effect;
  };
  reducers: {
    setText: Reducer<UserModelState>;
    signin: Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'user',
  state: {
    username: '',
    password: '',
  },

  effects: {
    *login({ payload }, { call, put }) {
      const res = yield call(login, payload);
      if (res && res.httpStatus === 200) {
        console.log(history.push('/home'));
      } else {
        message.warn(res.msg);
      }
    },
  },
  reducers: {
    setText(state) {
      return {
        ...state,
        text: 'setted dva',
      };
    },
    signin(state) {
      return {
        ...state,
        login: true,
      };
    },
  },
};

export default UserModel;
